
import sun.reflect.generics.tree.Tree;

import java.util.*;

public class Solution4 {
    public static void main(String[] args) {
//        TreeNode t1= new TreeNode(1);
//        TreeNode t2= new TreeNode(2);
//        TreeNode t3= new TreeNode(3);
//        TreeNode t4= new TreeNode(4);
//        TreeNode t5= new TreeNode(5);
//        t1.left = t2;
//        t1.right = t3;
//        t3.left = t4;
//        t3.right = t5;
//        Solution4 solution4= new Solution4();
//        String serialize = solution4.serialize(t1);
//        System.out.println(serialize);
//
//        TreeNode treeNode = solution4.deserialize(serialize);

        TreeNode node1 =new TreeNode(1);
        TreeNode node2 = new TreeNode(1);
        TreeNode node3 = node1;
        node3.val = 2;
        System.out.println(node3.val);
        System.out.println(node1.val);
        if(node1 == node3){
            System.out.println("true");
        }else{
            System.out.println("false");
        }


    }

    private static final String spliter = ",";
    private static final String NN = "null";

    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        StringBuilder sb = new StringBuilder();
        buildString(root, sb);
        return sb.toString();
    }

    private void buildString(TreeNode node, StringBuilder sb) {
        if (node == null) {
            sb.append(NN).append(spliter);
        } else {
            sb.append(node.val).append(spliter);
            buildString(node.left, sb);
            buildString(node.right,sb);
        }
    }
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        Deque<String> nodes = new LinkedList<>();
        String[] split = data.split(spliter);
        nodes.addAll(Arrays.asList(data.split(spliter)));
        return buildTree(nodes);
    }

    private TreeNode buildTree(Deque<String> nodes) {
        String val = nodes.remove();
        if (val.equals(NN)) return null;
        else {
            TreeNode node = new TreeNode(Integer.valueOf(val));
            node.left = buildTree(nodes);
            node.right = buildTree(nodes);
            return node;
        }
    }
}
